/*
 * MainWindow.cpp
 *
 *  Created on: 2012-8-28
 *      Author: yangentao@gmail.com
 */

#include <windows.h>

#include "MainWindow.h"
#include "../winui/ui.h"
#include "../util/util.h"

using namespace ui;

int Scintilla_RegisterClasses(void* hInstance);

MainWindow::MainWindow() {
	edit = 0;
	frame.addMessageHandler(&adapter);
	frame.addFinishHandler(boost::bind(&MainWindow::OnFinished, this, _1));
	frame.addCreatingHandler(boost::bind(&MainWindow::onCreating, this, _1, _2));

	adapter.OnMenuItem = boost::bind(&MainWindow::onMenuItem, this, _1);
	adapter.OnKeyDown = boost::bind(&MainWindow::onKeyDown, this, _1, _2, _3, _4, _5, _6, _7);
	adapter.OnCreateChildren = boost::bind(&MainWindow::onCreateChildren, this);
	adapter.OnLayoutChildren = boost::bind(&MainWindow::onLayoutChildren, this);

}

MainWindow::~MainWindow() {
	frame.clearWindowListeners();
}

void MainWindow::createEdit() {
	HINSTANCE inst = ::GetModuleHandle(0);
	Scintilla_RegisterClasses(inst);
	edit = ::CreateWindowEx(0, L"Scintilla", L"hello", WS_CHILD | WS_VISIBLE, 0, 0, 400, 300, frame.getWnd(), NULL, inst, NULL);

}

HWND MainWindow::create(int x, int y, int width, int height) {
	frame.create(L"Hello", 100, 100, 500, 400);
	frame.showWindow(SW_SHOW);
	frame.updateWindow();
	return frame.getWnd();
}
void MainWindow::close() {
	frame.closeWindow();
}

HMENU MainWindow::createMainMenu() {
	MenuBuilder mFile;
	mFile.CreateAsSub();
	mFile.AppendItem(L"file.new", L"New\tCtrl+N");
	mFile.AppendItem(L"file.open", L"Open\tCtrl+O");
	mFile.AppendItem(L"file.save", L"Save\tCtrl+S");
	mFile.AppendItem(L"file.saveas", L"Save As...");
	mFile.AppendItem(L"file.saveall", L"Save All\tCtrl+Shift+S");
	mFile.AppendSeprator();
	mFile.AppendItem(L"file.exit", L"Exit\tAlt+X");

	MenuBuilder mEdit;
	mEdit.CreateAsSub();
	mEdit.AppendItem(L"edit.copy", L"Copy");
	mEdit.AppendItem(L"edit.cut", L"Cut");
	mEdit.AppendItem(L"edit.paste", L"Paste");
	mEdit.AppendSeprator();
	mEdit.AppendItem(L"edit.redo", L"Redo");
	mEdit.AppendItem(L"edit.undo", L"Undo");

	MenuBuilder mWindow;
	mWindow.CreateAsSub();
	mWindow.AppendItem(L"buffers.close", L"Close");
	mWindow.AppendItem(L"buffers.closeother", L"Close Other");
	mWindow.AppendItem(L"buffers.closeall", L"Close All");
	mWindow.AppendSeprator();

	MenuBuilder mHelp;
	mHelp.CreateAsSub();
	mHelp.AppendItem(L"help.about", L"About");

	MenuBuilder mTest;
	mTest.CreateAsSub();
	mTest.AppendItem(L"test.one", L"One");
	mTest.AppendItem(L"test.two", L"Two");
	mTest.AppendItem(L"test.three", L"Three");
	mTest.AppendItem(L"test.four", L"Four");
	mTest.AppendItem(L"test.five", L"Five");

	MenuBuilder menuBar;
	menuBar.Create();
	menuBar.AppendSubMenu(L"File", mFile);
	menuBar.AppendSubMenu(L"Edit", mEdit);
	menuBar.AppendSubMenu(L"Window", mWindow);
	menuBar.AppendSubMenu(L"Help", mHelp);
	menuBar.AppendSubMenu(L"Test", mTest);

	return menuBar;
}

void MainWindow::OnFinished(HWND w) {
}
void MainWindow::onCreating(HWND w, CREATESTRUCT* createStruct) {
}
void MainWindow::onMenuItem(UINT menuID) {
	if (menuID == MenuBuilder::ID(L"file.new")) {
		Log << "file.new" << endl;
	} else if (menuID == MenuBuilder::ID(L"file.exit")) {
		Log << "file.exit" << endl;
		frame.closeWindow();
	} else if (menuID == MenuBuilder::ID(L"test.one")) {
		Log << "test.one" << endl;
	} else if (menuID == MenuBuilder::ID(L"test.two")) {
		Log << "test.two" << endl;
		String s = "abc";
		String ss = s;
		Log << (int) s.getBufferRead() << endl;
		Log << (int) ss.getBufferRead() << endl;
		ss.append('d');
		Log << (int) s.getBufferRead() << endl;
		Log << (int) ss.getBufferRead() << endl;

	}
}
void MainWindow::onCreateChildren() {
	createEdit();
	HMENU menu = createMainMenu();
	frame.setMenu(menu);

}
void MainWindow::onLayoutChildren() {
	edit.fillParentClientArea();
}

void MainWindow::onKeyDown(bool altDown, bool ctrlDown, bool shiftDown, DWORD vKey, DWORD lParam, BOOL isRepeat, BOOL& handled) {
	Log << "KeyDown: alt=" << altDown << " ctrl=" << ctrlDown << " shift=" << shiftDown << " Key=" << vKey << endl;
}
